package com.anyun.libusbcamera;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.safeluck.floatwindow.app.MyApplication;
import com.safeluck.floatwindow.bean.GlobalData;
import com.safeluck.floatwindow.usbVideo.WaterMakeUtils;
import com.safeluck.floatwindow.util.MyLog;

import java.nio.ByteBuffer;
import java.util.Date;


public class UsbCamera {
    static {
        System.loadLibrary("usbcamera-lib");
    }
private static final String TAG = "USBCamera";

    public native void rgba(int type,byte[] output);
    public native int prepareCamera(int videoid, int[] resolution,boolean ay_camera);
    public native int prepareCameraWithBase( int videoid, int videobase, int[] resolution,boolean ay_camera);
    public native int processCamera();
    public native void stopCamera();
    public native void pixeltobmp( Bitmap bitmap);
    public native void setenv();
    public native String getPixelFormat();
    public native UsbCameraInfo[] getUsbCameraInfo();
    public void MJPGToRGBA(byte []mjpg, byte []rgba) {
        if (mjpg.length == 0)
            return;

        Bitmap bitmap = BitmapFactory.decodeByteArray(mjpg, 0, mjpg.length);

        if (bitmap != null) {
            int width, height;

            width=bitmap.getWidth();
            height=bitmap.getHeight();

//            Log.d(TAG, "width = " + width + " height = " + height);

            ByteBuffer buffer= ByteBuffer.allocate(width * height * 4);
            bitmap.copyPixelsToBuffer(buffer);

            byte []out = buffer.array();

            System.arraycopy(out, 0, rgba, 0, out.length);
        } else {
            Log.e(TAG, "MJPG data error");
        }
    }
    public int readFrames(){
        for (int i = 0; i < 11; i++) {
            processCamera();
        }
        return processCamera();
    }

    public void Watermark(byte []rgba, byte []rgba2, int width, int height)
    {
        Log.i(TAG,"watermask="+width+":"+height);

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        ByteBuffer bf = ByteBuffer.wrap(rgba);
        bitmap.copyPixelsFromBuffer(bf);

        Bitmap wbmp = WaterMakeUtils.AddWaterMake(MyApplication.getContext(), 0, bitmap, width,
                GlobalData.getInstance().parseWaterMaskInfo("school","无", GlobalData.ShareType.STRING),
                GlobalData.getInstance().parseWaterMaskInfo("teacher","无", GlobalData.ShareType.STRING),
                GlobalData.getInstance().parseWaterMaskInfo("student","无", GlobalData.ShareType.STRING),
                GlobalData.getInstance().parseWaterMaskInfo("speed",0.0, GlobalData.ShareType.DOUBLE),
                GlobalData.getInstance().parseWaterMaskInfo("car_license","无", GlobalData.ShareType.STRING),
                GlobalData.getInstance().parseWaterMaskInfo("latitude",29.51228918, GlobalData.ShareType.DOUBLE),
                GlobalData.getInstance().parseWaterMaskInfo("longitude",106.45556208, GlobalData.ShareType.DOUBLE),
                new Date());

        ByteBuffer bf2 = ByteBuffer.allocate(width * height * 4);
        wbmp.copyPixelsToBuffer(bf2);

        byte []out = bf2.array();

        System.arraycopy(out, 0, rgba2, 0, out.length);
    }
}
